<?php


namespace app\common;



use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Excel {

    private $arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

    /**
     * 创建对象
     * @param string $end
     * @param array $fieldName
     * @return array
     */
    public function createExcel(string $end, array $fieldName) {
        //创建一个新的excel文档
        $newExcel = new Spreadsheet();
        //获取当前操作sheet的对象
        $objSheet = $newExcel->getActiveSheet();
        //设置宽度为true,不然太窄了
        foreach ($this->arr as $k => $value) {
            $newExcel->getActiveSheet()->getColumnDimension($value)->setAutoSize(true);
            $objSheet->setCellValue($value . '1', $fieldName[$k]);
            if ($value === $end) {
                break;
            }
        }
        return ['excel' => $newExcel, 'objSheet' => $objSheet];
    }
}